package com.huilian.iotbox.data.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dao.LogManipulateDao;
import com.huilian.iotbox.data.dto.LogLoginDto;
import com.huilian.iotbox.data.enums.OperationLogEnum;
import com.huilian.iotbox.data.po.LogManipulate;
import com.huilian.iotbox.data.service.LogManipulateService;
import com.huilian.iotbox.data.utils.DateTimeUtils;
import com.huilian.iotbox.data.utils.Util;
import com.huilian.iotbox.data.vo.LogManipulateVo;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 谢洛涛
 * @date 2021/5/17 16:22
 */
@Service
public class LogManipulateServiceImpl implements LogManipulateService {
    @Resource
    private LogManipulateDao logManipulateDao;

    @Override
    public Integer insert(UserInfo userInfo, String param, String remark, HttpServletRequest request, ServerResponse response, String deviceNo, Integer type) {
        LogManipulate logManipulate = new LogManipulate();
//        logManipulate.setUserName(userInfo.getUsername());
        logManipulate.setUserId(userInfo.getId());
        logManipulate.setIp(Util.getIpAddress(request));
        logManipulate.setPlace(null);
        Map<String, String> map = Util.getDevice(request.getHeader("User-Agent"));
        logManipulate.setBrowser(map.get("browser"));
        logManipulate.setSystem(map.get("system"));
        logManipulate.setCreateTime(new Date());
        if (!StringUtils.isEmpty(param) && param.length() > 5000) {
            param = param.substring(0, 5000);
        }
        logManipulate.setParam(param);
        logManipulate.setType(type);
        if (!StringUtils.isEmpty(deviceNo)) {
            logManipulate.setDeviceNo(deviceNo);
        }
        if (OperationLogEnum.STATUS_SUCC.getCode().equals(Util.stringConvert(response.getStatus()))) {
            logManipulate.setStatus(OperationLogEnum.STATUS_SUCC.getCode());
        } else {
            logManipulate.setStatus(OperationLogEnum.STATUS_FAIL.getCode());
        }
        logManipulate.setRemark(remark);
        return logManipulateDao.insert(logManipulate);
    }

    @Override
    public PageInfo findAllByKeyword(LogManipulate logManipulate) {
        PageHelper.startPage(logManipulate.getPageNum(), logManipulate.getPageSize(), " lm.createTime desc");
        List<LogManipulateVo> list = logManipulateDao.findAllByKeyword(logManipulate);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public ServerResponse delete(Integer id) {
        logManipulateDao.delete(id);
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse batchDelete(LogLoginDto logLoginDto) {
        logManipulateDao.batchDelete(logLoginDto.getBatchDeleteToList());
        return ServerResponse.createBySuccess();
    }

    @Override
    public ServerResponse deleteInStartAndEndDateTime(LogLoginDto logLoginDto) {
        logManipulateDao.deleteInStartAndEndDateTime(logLoginDto);
        return ServerResponse.createBySuccess();
    }

    @Override
    public Integer deleteByTime() {
        String time = DateTimeUtils.getSomeMonthBeforeEndTime(2);
        return logManipulateDao.deleteByTime(time);
    }

    @Override
    public ServerResponse findListByKeyword(LogManipulate logManipulate) {
        PageHelper.startPage(logManipulate.getPageNum(), logManipulate.getPageSize(), false);
        List<LogManipulateVo> logManipulatesList = logManipulateDao.findAllByKeyword(logManipulate);
        PageInfo pagResult = new PageInfo(logManipulatesList);
        if (logManipulatesList.size() < 10) {
            pagResult.setTotal(logManipulate.getPageSize() * logManipulate.getPageNum());
        } else {
            pagResult.setTotal(logManipulate.getPageSize() * logManipulate.getPageNum() + 1);
        }
        return ServerResponse.createBySuccess(pagResult);
    }

    @Override
    public void add(LogManipulate logManipulate) {
        logManipulateDao.insert(logManipulate);
    }
}
